package com.service.Impl;
import java.util.ArrayList;
import java.util.List;

import com.dao.OwnerMapper;
import com.entity.Owner;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.OwnerService;
import com.utils.BaseService;
import com.utils.DataTables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class OwnerServiceImpl extends BaseService implements OwnerService{
    @Autowired
    private OwnerMapper ownerMapper;
    @Override
    public long getOwnerRowCount(){
        return ownerMapper.getOwnerRowCount();
    }
    @Override
    public List<Owner> selectOwner(){
        return ownerMapper.selectOwner();
    }
    @Override
    public Owner selectOwnerByObj(Owner obj){
        return ownerMapper.selectOwnerByObj(obj);
    }
    @Override
    public Owner selectOwnerById(Integer id){
        return ownerMapper.selectOwnerById(id);
    }
    @Override
    public int insertOwner(Owner value){
        return ownerMapper.insertOwner(value);
    }
    @Override
    public int insertNonEmptyOwner(Owner value){
        return ownerMapper.insertNonEmptyOwner(value);
    }
    @Override
    public int insertOwnerByBatch(List<Owner> value){
        return ownerMapper.insertOwnerByBatch(value);
    }
    @Override
    public int deleteOwnerById(Integer id){
        return ownerMapper.deleteOwnerById(id);
    }
    @Override
    public int updateOwnerById(Owner enti){
        return ownerMapper.updateOwnerById(enti);
    }
    @Override
    public int updateNonEmptyOwnerById(Owner enti){
        return ownerMapper.updateNonEmptyOwnerById(enti);
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("sort asc");

        //判断需要排序的类是否为空
        if(!StringUtils.isEmpty(dataTables.getColumn())){
            String column = propertyToColumn(OwnerMapper.class, dataTables.getColumn());
            if(!StringUtils.isEmpty(column)){
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }

        PageInfo<Owner> pageInfo = new PageInfo<Owner>(ownerMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<Owner>());

        return dataTables;
    }

    public OwnerMapper getOwnerMapper() {
        return this.ownerMapper;
    }

    public void setOwnerMapper(OwnerMapper ownerMapper) {
        this.ownerMapper = ownerMapper;
    }

}